package com.jeeplus.modules.sys.web;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.DataRule;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.ReportService;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.test.one.dialog.entity.Leave1;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: ReportController
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/7/1513:15
 */
@Controller
@RequestMapping(value = "${adminPath}/Report")
public class ReportController extends BaseController {
    @Autowired
    ReportService reportService;
    @Autowired
    OfficeService officeService;
    @RequiresPermissions("report:list")
    @RequestMapping(value = {"list", ""})
    public String list(Leave1 leave1,Model model) {
        model.addAttribute("leave1", leave1);
        return "modules/Report/test";
    }
    /*获取机构信息*/
    protected String getOfficeCode(String officeId)
    {
        List<String> list=new ArrayList<>();
        for (Office off:UserUtils.getOfficeList()){
            for (String p:officeId.split(",")){
                if(off.getId().contains(p)){
                    if(!list.contains(off.getCode())&&!off.getCode().isEmpty())
                    {
                        list.add(off.getCode());
                    }
                }
            }
        }
        return StringUtils.join(list.toArray(),",");
    }
    @ResponseBody
    @RequiresPermissions("report:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(Leave1 leave1, HttpServletRequest request, HttpServletResponse response, Model model) {
        if(!leave1.getOffice().getId().isEmpty()){
            System.out.println(getOfficeCode(leave1.getOffice().getId()));
        }
        Page<Leave1> page = reportService.findPage(new Page<Leave1>(request, response),leave1);
        System.out.println(getBootstrapListData(page));
        return getBootstrapListData(page);
    }
}
